package com.lanhao.aot.carrierintegration.willis;

import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.lanhao.aot.util.RSAUtils;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

public class WillisIntegrationUtil {
	public static final String BASE_URL = "http://140.143.154.174:7003/inforomNew";
	public static final String KeyId = "6f697a2f-b896-4851-a96a-6982c0b91344";
	public static final String BusinessCode = "01006";
	public static final String username = "aliexpress2020";
	public static final String password = "Ali2@2@123";
	
	public static final String StoreCode = "WMWHSE15";
	
	public static final String TokenURL = "/rest/authLogin.vc";
	public static final String getLabelUrl = "http://localhost:8181/smt-integration/willis/v1/getLogisticsLabel";
	public static final String ahPublicKeyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArFBPSycbAjItIWkgoCRbrn39paro7LurJrNeESrTGRdXWeXImZy8I7QAY5eB8RPnYW9sdTk1ulLiNlHazQ+OCv6PgGXhxd23wu7Gvx+GBoZagHdpCcYkAio01hX6TyIDHjQ7/d7aT3NFvGvJ/p8lTqFOIGMjwH1qum2yDeL5HDfSA9wcO6m6oQFlqZjLJvvQfP9a9ktiOakk1Z03bZ26UOVbJrWOJNUCVl76b8HW7FvJQQFtG0qUuU7I0/VjyGXthFl0NOW/A9adsjMwb8zTq/VUApDYadWWye3k4D/MQQ9R6hxWCnVoGuLxisMWbaHaYFT/S6YzCEGRVWa1OWEA2wIDAQAB";
	
	public static void main(String[] args) {
//		String loginToken = getLoginToken();
//		System.out.println(loginToken);
		getLabel();
	}
	
	public static String getLoginToken() {
		Map<String, String> requestHeader = getRequestHeader(null);
		JSONObject request = new JSONObject();
		request.put("username", username);
		request.put("password", password);
		String respBody = HttpRequest.post(BASE_URL + TokenURL).addHeaders(requestHeader).body(request.toJSONString()).execute().body();
		// {"username":"aliexpress2020","expired":"2021-07-02 17:19:17","token":"897e72f44c423f71636e01add7f135a4","status":"SUCCESS"}
		// 有效期30分钟
		return respBody;
	}
	
	public static String getLabel() {
		try {
			PublicKey ahPublicKey = RSAUtils.getPublicKey(ahPublicKeyString);
			WillisLabelRequestDTO labelRequest = new WillisLabelRequestDTO();
			labelRequest.setTaskNo(UUID.fastUUID().toString().replaceAll("-", ""));
			labelRequest.setShipperArea("");
			labelRequest.setShipperCity("Birmingham");
			labelRequest.setShipperName("Mr. Lee");
			labelRequest.setShipperPhone("+44 (0)121 331 5000");
			labelRequest.setShipperPostcode("B5 5JU");
			labelRequest.setShipperProvince("England");
			labelRequest.setShipperStreet("Birmingham City University University House 15 Bartholomew Row");
			
			labelRequest.setAddresseeArea("");
			labelRequest.setAddresseeCity("London");
			labelRequest.setAddresseeName("UK-RTN-GT");
			labelRequest.setAddresseePhone("07405255344");
			labelRequest.setAddresseePostcode("E1 1RD");
			labelRequest.setAddresseeProvince("England");
			labelRequest.setAddresseeStreet("Alliance station ltd( AliExpress Return service), Suite 126,145-147 Boston Road, Hanwell,");
			labelRequest.setLocationID("CKUKLD0001");
			labelRequest.setLogisticsCompanyCode("Royal Mail");
			labelRequest.setCountry("UK");
			labelRequest.setLogisticsMode("");
			String reuqestString = JSON.toJSONString(labelRequest);
			System.out.println("请求入参request=" + reuqestString);
			String encryptRequest = RSAUtils.encrypt(reuqestString, ahPublicKey);
			System.out.println("使用安和公钥加密后的request=" + encryptRequest);
			String result = HttpUtil.post(getLabelUrl, encryptRequest);
			System.out.println("收到远程调用结果result=" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "Success";
	}
	
	public static Map<String, String> getRequestHeader(String token) {
		HashMap<String,String> headerMap = Maps.newHashMap();
		headerMap.put("KeyId", KeyId);
		headerMap.put("BusinessCode", BusinessCode);
		if (token != null) {
			headerMap.put("token", token);
		}
		
		return headerMap;
	}
	
	
	
	
}
